package com.zzp.sms.security.service;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
/**
 * @author 佐斯特勒
 * <p>
 *  人皆知有用之用，而莫知无用之用也
 * </p>
 * @version v1.0.0
 * @date 2020/1/13 14:21
 * @see  SmsCodeService
 **/
@Service
public class SmsCodeService {

    private static Logger logger = LoggerFactory.getLogger(SmsCodeService.class);

    @Value("${aliyun.dev.accessKeyID}")
    private String accessKeyID;

    @Value("${aliyun.dev.accessKeySecret}")
    private String accessKeySecret;

    public void sendMsg(String code, String phoneNum) {
        logger.info("手机号为：" + phoneNum + ", 短信验证码: " + code);
        // 调用阿里短信接口
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyID, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", "xxx");
        request.putQueryParameter("TemplateCode", "xxx");
        request.putQueryParameter("TemplateParam", "{\"code\":\"验证码\"}");
        try {
            var response = client.getCommonResponse(request);
            logger.debug(response.getData());
            // 发送完短信，要入库
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
